const db = require('../db/index')
const {echartBubble} = require('../utils/index')

//气泡图
exports.bubble = (req,res)=>{
   db.query( `select count(*) as size,rt.type as name FROM reimbursement as r inner join reimbursement_types as rt on  r.type_id = rt.id and r.user_id=? group by rt.type having count(1)>1 limit 0,4`,req.user.id,(err,result)=>{

      if(err) return res.send({
         code:500,
         message:"查询失败",
         data:err
      })

      res.send({
         code:200,
         message:'查询成功',
         data:echartBubble(result)
      })

   })
}

//进度条
exports.round = (req,res)=>{

   let strsql1 = 'select count(*) as num1 from reimbursement as r inner join audit_control as ac on r.id = ac.p_id and r.user_id=? and r.is_delete=0'
   let strsql2 = 'select count(*) as num2 from reimbursement as r inner join audit_control as ac on r.id = ac.p_id and r.user_id=? and r.is_delete=0 and ac.state=?'

   let pending = new Promise((resolve,reject)=>{//待报销数
      db.query(strsql2,[req.user.id,0],(err,result)=>{
         if(err) return reject(err)
         resolve(result[0].num2)
      })
   })

   let complete = new Promise((resolve,reject)=>{//已报销数
      db.query(strsql2,[req.user.id,1],(err,result)=>{
         if(err) return reject(err)
         resolve(result[0].num2)
      })
   })

   let overrule = new Promise((resolve,reject)=>{//已驳回数
      db.query(strsql2,[req.user.id,2],(err,result)=>{
         if(err) return reject(err)
         resolve(result[0].num2)
      })
   })


   //待报销
   let p1 = new Promise((resole,reject)=>{
      db.query(strsql1,req.user.id,(err,result)=>{
         if(err) return reject(err)
         let num1 = result[0].num1
         db.query(strsql2,[req.user.id,0],(err,result)=>{
            if(err) return reject(err)
            if(result[0].num2===0) return resole({
               name:'待报销',
               data:0
            })
            resole({
               name:'待报销',
               data:result[0].num2/num1
            })
         })
      })
   })

   //已报销
   let p2 = new Promise((resole,reject)=>{
      db.query(strsql1,req.user.id,(err,result)=>{
         if(err) return reject(err)
         let num1 = result[0].num1
         db.query(strsql2,[req.user.id,1],(err,result)=>{
            if(err) return reject(err)
            if(result[0].num2===0) return resole({
               name:'待报销',
               data:0
            })
            resole({
               name:'已报销',
               data:result[0].num2/num1
            })
         })
      })
   })

   //已驳回
   let p3 = new Promise((resole,reject)=>{
      db.query(strsql1,req.user.id,(err,result)=>{
         if(err) return reject(err)
         let num1 = result[0].num1
         db.query(strsql2,[req.user.id,2],(err,result)=>{
            if(err) return reject(err)
            if(result[0].num2===0) return resole({
               name:'已驳回',
               data:0
            })
            resole({
               name:'已驳回',
               data:result[0].num2/num1
            })
         })
      })
   })

   Promise.all([pending,complete,overrule,p1,p2,p3]).then((result)=>{
      res.send({
         code:200,
         message:'查询成功',
         data:{
            total:{
               pending:result[0],
               complete:result[1],
               overrule:result[2]
            },
            series:result.splice(3,5)
         }
      })
   }).catch(err=>{
      res.send({
         code:500,
         message:"查询失败",
         data:err
      })
   })

}


//统计
exports.total = (req,res)=>{

   let strsql1 = "select * from reimbursement as r inner join audit_control as ac on r.id = ac.p_id and r.user_id=? and r.is_delete = 0 and ac.state = ?"

   let strsql2 = 'select sum(r.price) as price from reimbursement as r inner join audit_control as ac on r.id = ac.p_id and r.user_id=? and r.is_delete=0 and ac.state=?'

   let pending = new Promise((resolve,reject)=>{//待报销金额
      db.query(strsql1,[req.user.id,0],(err,result)=>{

         if(err) return reject(err)

         if(result.length===0) return resolve(0)

         db.query(strsql2,[req.user.id,0],(err,result)=>{
            if(err) return reject(err)
            resolve(result[0].price)
         })

      })
   })

   let complete = new Promise((resolve,reject)=>{//已报销金额
      db.query(strsql1,[req.user.id,1],(err,result)=>{

         if(err) return reject(err)

         if(result.length===0) return resolve(0)

         db.query(strsql2,[req.user.id,1],(err,result)=>{
            if(err) return reject(err)
            resolve(result[0].price)
         })

      })
   })

   let overrule = new Promise((resolve,reject)=>{//被驳回金额
      db.query(strsql1,[req.user.id,2],(err,result)=>{

         if(err) return reject(err)

         if(result.length===0) return resolve(0)

         db.query(strsql2,[req.user.id,2],(err,result)=>{
            if(err) return reject(err)
            resolve(result[0].price)
         })

      })

   })

   Promise.all([pending,complete,overrule]).then(result=>{
      res.send({
         code:200,
         message:'查询成功',
         data:{
            pending:result[0],
            complete:result[1],
            overrule:result[2]
         }
      })
   }).catch(err=>{
      res.send({
         code:500,
         message:'查询失败',
         data:err
      })
   })

}
